package et.six.eta.View;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;

import java.util.ArrayList;
import java.util.List;

import et.six.eta.Adapter.ListAdapter2;
import et.six.eta.Ban.FoodList;
import et.six.eta.MainActivity;
import et.six.eta.R;

public class zhujiemian_fenlei extends AppCompatActivity implements ReflashListview.IReflashListener {
    String food_Name;
    String food_Price;
    String food_Img;
    ReflashListview lv;
    ArrayList<FoodList> lxrs;
    TextView tv1, fanhui;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhujiemian_fenlei);

        lv = findViewById(R.id.lv_zjmitem);
        lv.setInterface(this);
        tv1 = findViewById(R.id.tv1);
        fanhui = findViewById(R.id.fanui);
        select();

        fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(zhujiemian_fenlei.this, MainActivity.class);
                startActivity(it);
            }
        });
    }


    public void select() {
        Intent it = getIntent();
        String tiaojian = it.getStringExtra("acc");
        tv1.setText(tiaojian);
        lxrs = new ArrayList<FoodList>();
        AVQuery<AVObject> query = new AVQuery<>("FoodList");
        query.whereEqualTo("food_Type", tiaojian);
        query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e) {
                for (int i = 0; i < list.size(); i++) {
                    FoodList foodList = new FoodList();
                    food_Img = list.get(i).getString("food_Img");
                    food_Name = list.get(i).getString("food_Name");
                    food_Price = list.get(i).getString("food_Price");
                    foodList.setFood_Img(food_Img);
                    foodList.setFood_name(food_Name);
                    foodList.setFood_Price(food_Price);
                    lxrs.add(foodList);
                }
                ListAdapter2 adapter = new ListAdapter2(lxrs, zhujiemian_fenlei.this);
                lv.setAdapter(adapter);
            }
        });
    }

    @Override
    public void onReflash() {
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                select();//获取最新数据
                recreate();
                Toast.makeText(zhujiemian_fenlei.this,"刷新成功",Toast.LENGTH_SHORT).show();
            }
        }, 2000);

    }
}

